package com.jasiri.project.utils.dialog

import android.content.Context
import android.text.SpannableString
import android.text.style.StrikethroughSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.jasiri.project.R
import com.jasiri.project.app.App
import com.jasiri.project.databinding.DialogLimitImproveBinding
import com.jasiri.project.pro.ProDialog
import com.jasiri.project.utils.DensityUtils
import com.jasiri.project.utils.event.EventType
import com.jasiri.project.utils.gilde.ImageUtil
import com.jasiri.project.utils.http.HttpHelper
import org.json.JSONObject
import java.math.BigDecimal


/**
 * Sikadwa
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：产品额度提升dialog
 */
class LimitImproveDialog: ProDialog {
    private lateinit var binding: DialogLimitImproveBinding
    private var mCallback: IDialogCallback? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.getPhoneWidth() - DensityUtils.dp2px(32f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogLimitImproveBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {

    }

    override fun initListener() {
        setOnClickListener(binding.btnLimitImproveOk) //申请
        setOnClickListener(binding.btnLimitImproveCancel) //关闭
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.btnLimitImproveOk){
            mCallback?.onClick(v)
        }
    }

    /**
     * 设置数据
     */
    fun showLimitImprovedData(data: JSONObject){
        ImageUtil.instance?.loadImageRes(context,binding.imgLogo,R.mipmap.logo)
        binding.textAppName.text = App.instance().getString(R.string.app_name)
        binding.textImproveAmount.text = BigDecimal(data.optString("amount")).stripTrailingZeros().toPlainString()
        val text = BigDecimal(data.optString("oldAmount")).stripTrailingZeros().toPlainString()
        val content = SpannableString(text)
        content.setSpan(StrikethroughSpan(), 0, text.length, 0)
        binding.textOldAmount.text = content
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_LIMIT_IMPROVE_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_LIMIT_IMPROVE_DISMISS)
    }


    /**
     *
     * 接口描述：product额度相较比于上一次是否有变化
     * 接口名：/v1/amountIsUp
     * 参数：公共参数
     * 注意：贷超包product和token参数，传所需要查询的产品对应的值
     * 返回值：
     * openFlag，值"YES"或"NO"
     * 当openFlag=“YES”时，会多两个返回值：amount、oldAmount
     *
     */

}